package com.huaxin.hxmodulesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaxin.hxmodulesystem.common.CaptchaUtil;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.*;
import com.huaxin.hxmodulesystem.dto.response.ElectricianArchivesLoginRespDTO;
import com.huaxin.hxmodulesystem.models.ElectricianArchives;
import com.huaxin.hxmodulesystem.models.enums.AuditStatus;
import com.huaxin.hxmodulesystem.service.ElectricianArchivesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "电工档案")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/sys/electricianArchives")
public class ElectricianArchivesController {
    private final ElectricianArchivesService electricianArchivesService;
    private final CaptchaUtil captchaUtil;

    /**
     * 添加电工档案
     *
     * @param in
     * @return
     */
    @Operation(summary = "添加电工档案", description = "添加电工档案")
    @PostMapping("/addElectricianArchives")
    public ResponseResult<ElectricianArchives> addElectricianArchives(@RequestBody AddElectricianArchivesReqDTO in) {
        return electricianArchivesService.addElectricianArchives(in);
    }

    /**
     * 修改电工档案
     *
     * @param in
     * @return
     */
    @Operation(summary = "修改电工档案", description = "修改电工档案")
    @PostMapping("/updateElectricianArchives")
    public ResponseResult<String> updateElectricianArchives(@RequestBody UpdateElectricianArchivesReqDTO in) {
        return electricianArchivesService.updateElectricianArchives(in);
    }

    /**
     * 删除电工档案
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除电工档案", description = "删除电工档案")
    @GetMapping("/deleteElectricianArchives")
    public ResponseResult<String> deleteElectricianArchives(@Parameter(name = "id", description = "电工档案ID") @RequestParam(value = "id", required = true) String id) {
        return electricianArchivesService.deleteElectricianArchives(id);
    }

    /**
     * 获取电工档案列表
     *
     * @param pageNum     页码
     * @param pageSize    每页数量
     * @param workNumber  工号
     * @param realName    真实姓名
     * @param phone       手机号码
     * @param status      状态：0-正常；1-禁用
     * @param auditStatus 审核状态：0-待审核；1-审核通过；2-审核未通过
     * @return
     */
    @Operation(summary = "获取电工档案列表", description = "获取电工档案列表")
    @GetMapping("/getElectricianArchivesList")
    public ResponseResult<Page<ElectricianArchives>> getElectricianArchivesList(@Parameter(description = "页码", name = "pageNum") @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                                                @Parameter(description = "每页数量", name = "pageSize") @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                                                                @Parameter(description = "工号", name = "workNumber") @RequestParam(name = "workNumber", required = false) String workNumber,
                                                                                @Parameter(description = "真实姓名", name = "realName") @RequestParam(name = "realName", required = false) String realName,
                                                                                @Parameter(description = "手机号码", name = "phone") @RequestParam(name = "phone", required = false) String phone,
                                                                                @Parameter(description = "状态：0-正常；1-禁用；2-未认证", name = "status") @RequestParam(name = "status", required = false) Integer status,
                                                                                @Parameter(description = "审核状态：0-待审核；1-审核通过；2-审核未通过", name = "auditStatus") @RequestParam(name = "auditStatus", required = false) Integer auditStatus) {

        return electricianArchivesService.getElectricianArchivesList(pageNum, pageSize, workNumber, realName, phone, status, auditStatus);
    }

    /**
     * 获取电工档案详情
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取电工档案详情", description = "获取电工档案详情")
    @GetMapping("/getElectricianArchivesDetail")
    public ResponseResult<ElectricianArchives> getElectricianArchivesDetail(@Parameter(description = "电工档案ID", name = "id") @RequestParam(name = "id", required = true) String id) {
        return electricianArchivesService.getElectricianArchivesDetail(id);
    }

    /**
     * 电工登录
     *
     * @param in
     * @return
     */
    @Operation(summary = "电工登录", description = "电工登录")
    @PostMapping("/electricianLogin")
    public ResponseResult<ElectricianArchivesLoginRespDTO> electricianLogin(@RequestBody ElectricianLoginReqDTO in) {
        boolean isValid = captchaUtil.validateCaptcha(in.getVKey(), in.getVCode());
        if (!isValid) {
            return ResponseResult.verificationCodeError(null, "验证码错误或已过期");
        }
        return electricianArchivesService.electricianLogin(in);
    }

    /**
     * 电工档案审核通过
     *
     * @param in
     * @return
     */
    @Operation(summary = "电工档案审核通过", description = "电工档案审核通过")
    @PostMapping("/approve")
    public ResponseResult<Boolean> approve(@RequestBody ApproveElectricianReqDTO in) {
        AuditElectricianReqDTO auditElectricianReqDTO = AuditElectricianReqDTO.builder()
                .electricianArchiveId(in.getElectricianArchiveId())
                .auditStatus(AuditStatus.APPROVED)
                .comment(in.getComment())
                .build();
        return electricianArchivesService.audit(auditElectricianReqDTO);
    }

    /**
     * 电工档案审核拒绝
     *
     * @param in
     * @return
     */
    @Operation(summary = "电工档案审核拒绝", description = "电工档案审核拒绝")
    @PostMapping("/reject")
    public ResponseResult<Boolean> reject(@RequestBody RejectElectricianReqDTO in) {
        AuditElectricianReqDTO auditElectricianReqDTO = AuditElectricianReqDTO.builder()
                .electricianArchiveId(in.getElectricianArchiveId())
                .auditStatus(AuditStatus.REJECTED)
                .comment(in.getComment())
                .build();
        return electricianArchivesService.audit(auditElectricianReqDTO);
    }
}
